package com.gitee.situation.manager;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.situation.constant.Constant;
import com.gitee.situation.entity.DomesticData;
import com.gitee.situation.entity.ForeignStatisticsData;
import com.gitee.situation.entity.GlobalStatisticsData;
import com.gitee.situation.entity.SituationNews;
import com.gitee.situation.utils.HttpUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wq
 * @version 1.0
 * @date 2021/04/06 11:24
 */
@Component
public class SituationManager {

    /**
     * 获取新闻列表
     *
     * @return List<SituationNews>
     */
    public List<SituationNews> acquireNews() {
        String data = HttpUtils.doGet(Constant.SITUATION_URL);
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray newsList = jsonObject.getJSONArray("newslist");
        JSONObject listJSONObject = newsList.getJSONObject(0);
        JSONArray news = listJSONObject.getJSONArray("news");
        List<SituationNews> list = new ArrayList<>();
        for (int i = 0; i < news.size(); i++) {
            SituationNews situationNews = new SituationNews();
            String pubDateStr = news.getJSONObject(i).getString("pubDateStr");
            situationNews.setPubDateStr(pubDateStr);

            String title = news.getJSONObject(i).getString("title");
            situationNews.setTitle(title);

            String summary = news.getJSONObject(i).getString("summary");
            situationNews.setSummary(summary);

            String infoSource = news.getJSONObject(i).getString("infoSource");
            situationNews.setInfoSource(infoSource);

            String sourceUrl = news.getJSONObject(i).getString("sourceUrl");
            situationNews.setSourceUrl(sourceUrl);
            list.add(situationNews);
        }
        return list;
    }


    /**
     * 获取国内的数据
     *
     * @return List<DomesticData>
     */
    public DomesticData acquireDomesticData() {
        String data = HttpUtils.doGet(Constant.SITUATION_URL);
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray newsList = jsonObject.getJSONArray("newslist");
        JSONObject listJSONObject = newsList.getJSONObject(0);
        JSONObject desc = listJSONObject.getJSONObject("desc");
        DomesticData domesticData = new DomesticData();
        Integer currentConfirmedCount = desc.getInteger("currentConfirmedCount");
        domesticData.setCurrentConfirmedCount(currentConfirmedCount);

        Integer confirmedCount = desc.getInteger("confirmedCount");
        domesticData.setConfirmedCount(confirmedCount);

        Integer suspectedCount = desc.getInteger("suspectedCount");
        domesticData.setSuspectedCount(suspectedCount);

        Integer curedCount = desc.getInteger("curedCount");
        domesticData.setCuredCount(curedCount);

        Integer deadCount = desc.getInteger("deadCount");
        domesticData.setDeadCount(deadCount);

        Integer seriousCount = desc.getInteger("seriousCount");
        domesticData.setSeriousCount(seriousCount);

        Integer suspectedIncr = desc.getInteger("suspectedIncr");
        domesticData.setSuspectedIncr(suspectedIncr);

        Integer currentConfirmedIncr = desc.getInteger("currentConfirmedIncr");
        domesticData.setCurrentConfirmedIncr(currentConfirmedIncr);

        Integer confirmedIncr = desc.getInteger("confirmedIncr");
        domesticData.setConfirmedIncr(confirmedIncr);

        Integer curedIncr = desc.getInteger("curedIncr");
        domesticData.setCuredIncr(curedIncr);

        Integer deadIncr = desc.getInteger("deadIncr");
        domesticData.setDeadIncr(deadIncr);

        Integer seriousIncr = desc.getInteger("seriousIncr");
        domesticData.setSeriousIncr(seriousIncr);

        Integer yesterdayConfirmedCountIncr = desc.getInteger("yesterdayConfirmedCountIncr");
        domesticData.setYesterdayConfirmedCountIncr(yesterdayConfirmedCountIncr);

        Integer yesterdaySuspectedCountIncr = desc.getInteger("yesterdaySuspectedCountIncr");
        domesticData.setYesterdaySuspectedCountIncr(yesterdaySuspectedCountIncr);

        String remark1 = desc.getString("remark1");
        domesticData.setRemark1(remark1);

        String remark2 = desc.getString("remark2");
        domesticData.setRemark2(remark2);

        return domesticData;
    }


    /**
     * 获取国外数据
     *
     * @return ForeignStatisticsData
     */
    public ForeignStatisticsData acquireForeignData() {
        String data = HttpUtils.doGet(Constant.SITUATION_URL);
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray newsList = jsonObject.getJSONArray("newslist");
        JSONObject listJSONObject = newsList.getJSONObject(0);
        JSONObject desc = listJSONObject.getJSONObject("desc");
        JSONObject foreignStatistics = desc.getJSONObject("foreignStatistics");
        ForeignStatisticsData foreignStatisticsData = new ForeignStatisticsData();
        Integer currentConfirmedCount = foreignStatistics.getInteger("currentConfirmedCount");
        foreignStatisticsData.setCurrentConfirmedCount(currentConfirmedCount);

        Integer confirmedCount = foreignStatistics.getInteger("confirmedCount");
        foreignStatisticsData.setConfirmedCount(confirmedCount);

        Integer suspectedCount = foreignStatistics.getInteger("suspectedCount");
        foreignStatisticsData.setSuspectedCount(suspectedCount);

        Integer curedCount = foreignStatistics.getInteger("curedCount");
        foreignStatisticsData.setCuredCount(curedCount);

        Integer deadCount = foreignStatistics.getInteger("deadCount");
        foreignStatisticsData.setDeadCount(deadCount);

        Integer suspectedIncr = foreignStatistics.getInteger("suspectedIncr");
        foreignStatisticsData.setSuspectedIncr(suspectedIncr);

        Integer currentConfirmedIncr = foreignStatistics.getInteger("currentConfirmedIncr");
        foreignStatisticsData.setCurrentConfirmedIncr(currentConfirmedIncr);

        Integer confirmedIncr = foreignStatistics.getInteger("confirmedIncr");
        foreignStatisticsData.setConfirmedIncr(confirmedIncr);

        Integer curedIncr = foreignStatistics.getInteger("curedIncr");
        foreignStatisticsData.setCuredIncr(curedIncr);

        Integer deadIncr = foreignStatistics.getInteger("deadIncr");
        foreignStatisticsData.setDeadIncr(deadIncr);
        return foreignStatisticsData;
    }

    /**
     * 获取Global数据
     *
     * @return GlobalStatisticsData
     */
    public GlobalStatisticsData acquireGlobalData() {
        String data = HttpUtils.doGet(Constant.SITUATION_URL);
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray newsList = jsonObject.getJSONArray("newslist");
        JSONObject listJSONObject = newsList.getJSONObject(0);
        JSONObject desc = listJSONObject.getJSONObject("desc");
        JSONObject globalStatistics = desc.getJSONObject("globalStatistics");
        GlobalStatisticsData globalStatisticsData = new GlobalStatisticsData();
        Integer currentConfirmedCount = globalStatistics.getInteger("currentConfirmedCount");
        globalStatisticsData.setCurrentConfirmedCount(currentConfirmedCount);

        Integer confirmedCount = globalStatistics.getInteger("confirmedCount");
        globalStatisticsData.setConfirmedCount(confirmedCount);

        Integer curedCount = globalStatistics.getInteger("curedCount");
        globalStatisticsData.setCuredCount(curedCount);

        Integer deadCount = globalStatistics.getInteger("deadCount");
        globalStatisticsData.setDeadCount(deadCount);

        Integer currentConfirmedIncr = globalStatistics.getInteger("currentConfirmedIncr");
        globalStatisticsData.setCurrentConfirmedIncr(currentConfirmedIncr);

        Integer confirmedIncr = globalStatistics.getInteger("confirmedIncr");
        globalStatisticsData.setConfirmedIncr(confirmedIncr);

        Integer curedIncr = globalStatistics.getInteger("curedIncr");
        globalStatisticsData.setCuredIncr(curedIncr);

        Integer deadIncr = globalStatistics.getInteger("deadIncr");
        globalStatisticsData.setDeadIncr(deadIncr);

        Integer yesterdayConfirmedCountIncr = globalStatistics.getInteger("yesterdayConfirmedCountIncr");
        globalStatisticsData.setYesterdayConfirmedCountIncr(yesterdayConfirmedCountIncr);
        return globalStatisticsData;
    }
}
